package com.baosight.risk.web.system;

import com.baosight.risk.common.annotation.Log;
import com.baosight.risk.common.bean.BeanUtils;
import com.baosight.risk.common.enums.BusinessType;
import com.baosight.risk.common.result.AjaxResult;
import com.baosight.risk.api.system.ISysRoleService;
import com.baosight.risk.service.system.entity.SysRole;
import com.baosight.risk.base.controller.BaseController;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author duanyongqiang
 * @date 2019.3.29
 * <p>
 * 系统角色管理
 */

@Slf4j
@RestController
@RequestMapping("/system/role")
public class SysRoleController extends BaseController {

    @Autowired
    private ISysRoleService roleService;

    @Log(title = "角色查询")
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public AjaxResult select(SysRole role) {
        log.info("角色查询参数：" + role.toString());
        //startPage("create_time","desc");
        startPage("r.roleSort", "asc");
        return getResultPage(roleService.selectRole(role));
    }

    @PostMapping("save")
    @Log(title = "角色添加")
    public AjaxResult save(@RequestBody List<SysRole> roleList) throws Exception {
        if (CollectionUtils.isEmpty(roleList)) {
            return error("数据为空");
        }
        return toAjax(roleService.insertRole(roleList));
    }

//    @PostMapping("authorization")
//    @Log(title = "角色授权")
//    @RequiresPermissions("system:role:auth")
//    public AjaxResult authorization(SysRoleVO roleVO) {
//        if (StringUtils.isEmpty(roleVO.getRoleName()) ||
//                StringUtils.isEmpty(roleVO.getRoleCode()) ||
//                StringUtils.isEmpty(roleVO.getMenuMap()) ||
//                StringUtils.isEmpty(roleVO.getCompMap())) {
//            return error("数据有误");
//        }
//        log.info("角色授权：" + roleVO.toString());
//        return toAjax(roleService.authorization(roleVO));
//    }

    @GetMapping("edit")
    public AjaxResult edit(@RequestParam("id") String id) {
        return success(roleService.get(id));
    }

    @GetMapping("auth")
    public AjaxResult auth(@RequestParam("id") String id) {
        return success(roleService.get(id));
    }

    @PostMapping("update")
    @Log(title = "角色编辑")
    @RequiresPermissions("system:role:update")
    public AjaxResult update(@RequestBody SysRole role) {
        if (roleService.get(role.getId()) == null) {
            return error("数据不存在");
        }
        role = (SysRole) BeanUtils.updateBean(role);
        log.info("角色编辑：" + role.toString());
        return toAjax(roleService.updateRole(role));
    }

    @Log(title = "更新角色状态")
    @RequestMapping(value = "updateStatus", method = RequestMethod.PUT)
    public AjaxResult updateStatus(@RequestBody SysRole role) {
        SysRole sysRole = roleService.get(role.getId());
        if (sysRole == null) {
            return error("数据不存在");
        }
        sysRole.setStatus(role.getStatus());
        sysRole = (SysRole) BeanUtils.updateBean(sysRole);
        return toAjax(roleService.updateRole(sysRole));
    }

    @PostMapping("delete")
    @Log(title = "规则删除", businessType = BusinessType.DELETE)
    @RequiresPermissions("system:role:delete")
    public AjaxResult delete(@RequestBody Map<String, Object> map) {
        String ids = map.get("ids").toString();
        String status = map.get("status").toString();
        List<String> idss = JSONArray.fromObject(ids);
        List<SysRole> roles = roleService.selectRoleByIds(idss);
        if (idss.size() != roles.size()) {
            log.error("数据有误！");
        }
        return toAjax(roleService.delelteRoleByIds(idss, status));
    }

}
